<template>
  <div class="system-user-container app-container">
    <el-card>
      <div class="system-user-search mb15">
        <el-input v-model="state.listQuery.name" placeholder="请输入岗位名称" style="max-width: 180px"></el-input>
        <el-button type="primary" class="ml10" @click="search">查询
        </el-button>
        <el-button type="success" class="ml10" @click="onOpenSaveOrUpdate('save', null)">
          新增
        </el-button>
      </div>
      <z-table
          :columns="state.columns"
          :data="state.listData"
          ref="tableRef"
          v-model:page-size="state.listQuery.pageSize"
          v-model:page="state.listQuery.page"
          :total="state.total"
          @pagination-change="getList"
      />
    </el-card>
    <EditDuty @getList="getList" ref="SaveOrUpdateDutyRef"/>
  </div>
</template>

<script setup name="DutyList">
import {h, onMounted, reactive, ref} from 'vue';
import {ElButton, ElMessage, ElMessageBox, ElTag} from 'element-plus';
import EditDuty from '/@/views/hrms/duty/editDuty.vue';
import {dutyApi} from '/@/api/hrms/duty';

const SaveOrUpdateDutyRef = ref()
const tableRef = ref()

const state = reactive({
  columns: [
    {
      key: 'name', label: '岗位名称', width: '', align: 'center', show: true,
      render: ({row}) => h(ElButton, {
        link: true,
        type: "primary",
        onClick: () => {
          onOpenSaveOrUpdate("update", row)
        }
      }, () => row.name)
    },
    {key: 'type', label: '岗位类型', width: '', align: 'center', show: true},
    {key: 'level', label: '岗位等级', width: '', align: 'center', show: true},
    {key: 'remarks', label: '备注', width: '', align: 'center', show: true},
  ],
  // list
  listData: [],
  total: 0,
  listQuery: {
    page: 1,
    pageSize: 20,
    name: '',
  },
  //rule
  roleList: [],
  roleQuery: {
    page: 1,
    pageSize: 100,
  }
});
// 获取用户数据
const getList = () => {
  tableRef.value.openLoading()
  dutyApi().dutyList(state.listQuery)
      .then(res => {
        state.listData = res.data.rows
        state.total = res.data.rowTotal
      })
      .finally(() => {
        tableRef.value.closeLoading()
      })
};

// 查询
const search = () => {
  state.listQuery.page = 1
  getList()
}

// 新增或修改用户
const onOpenSaveOrUpdate = (editType, row) => {
  SaveOrUpdateDutyRef.value.openDialog(editType, row);
};

// 删除用户
const deleted = (row) => {
  ElMessageBox.confirm('是否删除该条数据, 是否继续?', '提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning',
  })
      .then(() => {
        dutyApi().dutyDeleted({id: row.id})
            .then(() => {
              ElMessage.success('删除成功');
              getList()
            })
      })
      .catch(() => {
      });
};

// 处理角色名称
const handleRoles = (roles) => {
  let roleTagList = []
  roles = roles ? roles : []
  roles.forEach((role) => {
    let roleName = state.roleList.find(e => e.id == role)?.name
    roleTagList.push(h(ElTag, null, () => roleName))
  })
  return h('div', null, roleTagList)
}
// 页面加载时
onMounted(() => {
  getList();
});

</script>
